package cn.edu.zjut.chopper.registry.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.zjut.chopper.constant.Constant;
import cn.edu.zjut.chopper.registry.Registry;
import cn.edu.zjut.chopper.registry.RegistryContext;
import cn.edu.zjut.chopper.registry.RegistryStatisticsThread;
import cn.edu.zjut.chopper.registry.moniter.Monitor;

/**
 * Servlet implementation class StopRegistry
 */
public class StopRegistry extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StopRegistry() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Registry registry = (Registry) RegistryContext.get(Constant.Registry);
		if(registry != null){
			registry.stopService();
		}
		Monitor monitor = (Monitor) RegistryContext.get(Constant.RegistryMonitor);
		if(monitor != null){
			monitor.stopMonitor();
		}
		RegistryStatisticsThread statistics = (RegistryStatisticsThread) RegistryContext.get(Constant.StatisticsThread);
		if(statistics != null){
			statistics.stopStatistics();
		}
		RegistryContext.removeAll();
		response.sendRedirect("/chopper/index.jsp");
	}

}
